package vn.sunnet.backviet01.game.coloa.util;

import java.util.Timer;
import java.util.TimerTask;

import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import vn.sunnet.backviet01.game.coloa.util.Contain.StateSkill;

public class MySkillButton extends Sprite{
	private boolean isUse = false;
	private StateSkill mState = StateSkill.NORMAL;
	
	private long delayEnemy = 0;
	
	private int idSkill = -1;
	
	public Sprite mMask;
	public Sprite mPlus;
	private Sprite mLightCircle;
	
	private CoolDownUse coolDown;
	private float useMana = 0;
	
	private float damage = 0;
	private int level = 1;
	
	public MySkillButton(float pX, float pY, TextureRegion pTextureRegion) {
		super(pX, pY, pTextureRegion);
		coolDown = new CoolDownUse();
	}
	
	public void setLevel(int level) {
		this.level = level;
	}
	
	public int getLevel() {
		return level;
	}
	
	public void resetInfo() {
		idSkill = -1;
		mState = StateSkill.NORMAL;
		isUse = false;
	}
	
	public long getDelayEnemy() {
		return delayEnemy;
	}

	public void setDelayEnemy(float delayEnemy) {
		this.delayEnemy = (long)(delayEnemy * 1000);
	}

	public float getDamage() {
		return damage;
	}

	public void setDamage(float damage) {
		this.damage = damage;
	}

	public float getUseMana() {
		return useMana;
	}

	public void setUseMana(float useMana) {
		this.useMana = useMana;
	}

	public void setIdSkill(int id) {
		idSkill = id;
	}
	
	public int getIdSkill() {
		return idSkill;
	}
	
	public void onManagedUpdate(float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
	}
	
	public void setDelay(final float delay) {
		coolDown.setDelay((long)(delay * 1000));
	}
	
	public void setMask(TextureRegion pTextureRegion) {
		mMask = new Sprite(8, 8, pTextureRegion);
		this.attachChild(mMask);
	}
	
	public void setPlus(TextureRegion pTextureRegion) {
		mPlus = new Sprite(7, 7, pTextureRegion);
		this.attachChild(mPlus);
	}
	
	public void setLightCircle(TextureRegion pTextureRegion) {
		mLightCircle = new Sprite(-14, -13, pTextureRegion);
		this.attachChild(mLightCircle);
	}
	
	public void displayLightCircle(boolean isDisplay) {
			mLightCircle.setVisible(isDisplay);
	}
	
	public void mask(boolean isMask) {
		mMask.setAlpha(1);
		mMask.setVisible(isMask);
	}
	
	public void plus(boolean isPlus) {
		mPlus.setVisible(isPlus);
	}
	
	public StateSkill getState() {
		return mState;
	}

	public void setState(StateSkill mState) {
		this.mState = mState;
	}

	public void setUse(boolean isUse) {
		this.isUse = isUse;
	}
	
	public void used(boolean use) {
		isUse = use;
		plus(!use);
		mask(!use);
		displayLightCircle(!use);
	}
	
	public boolean isUse() {
		return isUse;
	}

	public boolean checkUsable(final float pX, final float pY) {
		if (contains(pX, pY)) {
			return coolDown.checkValidity();
		}
		
		return false;
	}
	
	public void start() {
		mLightCircle.setVisible(true);
		mMask.setVisible(false);
//		coolDown.valid = true;
	}
	
	public void used() {
		coolDown.schedule();
		long  delay = coolDown.getDelay();
		mMask.clearEntityModifiers();
		AlphaModifier alphaModifier;
		if (delay > 0) {
			alphaModifier = new AlphaModifier(delay / 1000, 1.0f, 0);
			mLightCircle.setVisible(false);
			mMask.setVisible(true);
			mMask.registerEntityModifier(alphaModifier);
		} else {
			mLightCircle.setVisible(true);
			mMask.setVisible(false);
			
		}
	}

	@Override
	public boolean contains(final float pX, final float pY) {
		if(!this.isVisible()||!isUse)
			return false;
		else {
			return super.contains(pX, pY);
		}
	}
	
	class CoolDownUse {
		private long delay = 1000;
		private Timer timer;
		private boolean valid = true;

		CoolDownUse() {
			this.timer = new Timer();
			this.valid = true;
		}
		
		void setDelay(long delay) {
			this.delay = delay;
		}
		
		long getDelay() {
			return delay;
		}
		
		boolean checkValidity() {
			
			return valid;
		}
		
		void schedule() {
			this.valid = false;
			timer.schedule(new Task(), this.delay);
		}
		
		class Task extends TimerTask {

			public void run() {
				valid = true;
				mLightCircle.setVisible(true);
			}
		}
	}
}
